<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">

                            <button type="button" class="layui-btn layui-btn-primary layui-border-blue"
                                    id="data-fresh-btn"><i class="layui-icon layui-icon-refresh-1"></i> 刷 新
                            </button>
                        </div>
                        <div class="layui-inline">
                            <span class="layui-badge-rim">Error</span>
                            <div class="layui-input-inline">
                                <p>由于数据接口的原因，表格管理无法实现（localstroage）！</p>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="usertoolbar">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除</button>
            </div>
        </script>

        <table class="layui-hide" id="userTableId" lay-filter="userTableFilter"></table>

        <script type="text/html" id="switchTpl">
            <input type="checkbox" name="authoritys" value="{{d.authority}}" lay-skin="switch" lay-text="超级|普通"
                   lay-filter="toauthority" {{
                   d.authority== 1 ? 'checked' : '' }}>
        </script>

    </div>
</div>
<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        table.render({
            elem: '#userTableId',
            url: '/twei/getusers',
            method: "POST",
            toolbar: '#usertoolbar',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'layui-table-tip',
                icon: 'layui-icon-refresh-1'
            }],
            cols: [
                [
                    {type: "checkbox", width: 50},
                    {field: 'username', title: '用户名', sort: true},
                    {field: 'password', event: 'password', style: 'cursor: pointer;', title: 'MD5加密密码'},
                    {field: 'QQ', title: 'QQ'},
                    {
                        field: 'authority',
                        // event: 'authority',
                        title: '权限',
                        style: 'cursor: pointer;',
                        width: 100,
                        templet: '#switchTpl',
                        sort: true
                    },

                ]
            ],
            limits: [3, 6, 9],
            limit: 3,
            page: true,
            skin: 'line'
        });

        // 刷新的按钮实现
        $("#data-fresh-btn").click(function () {
            table.reload('ToneTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    one: ""
                }
            })
            $("#freshid").val("");
        });
        form.on('switch(toauthority)', function (result) {
            // true 1
            // false 0
            table.on('row(userTableFilter)', function (obj) {

                // 这个监听是先获取到当前行的数据，然后第二次监听才开始获取按钮的数据
                if (result.elem.checked === true) {
                    obj.data.authority = 1;
                } else {
                    obj.data.authority = 0;
                }
                console.log(result)
                $.ajax({
                    url: '/twei/toauthority',
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(obj.data),
                    success: function (data) {
                        layer.msg(data.msg, {icon: 1, time: 800});
                    }
                });
                console.log(obj.data)
            })

        });


    });
</script>
</body>
</html>